package servlet;

import com.alibaba.fastjson.JSON;
import until.BaseDao;
import until.BaseModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "DeleteNoticeServlet",urlPatterns = "/DeleteNotice")
public class DeleteNoticeServlet extends HttpServlet {
    BaseDao baseDao = new BaseDao();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = Integer.valueOf(req.getParameter("id"));
        String sql = "delete from t_notice where id="+id;
        //批量删除
        //String sql = "delete from t_notice where id in("+ids+")";
        //执行sql
        int result = baseDao.update(sql);
        BaseModel baseModel = new BaseModel();
        if(result>0){
            baseModel.setCode(1);
            baseModel.setMassage("删除公告成功");
        //baseModel.setMassage("删除"+result+"个公告成功");
        }else{
            baseModel.setCode(0);
            baseModel.setMassage("删除公告失败");
        }
        //返回JSON字符串
        String baseModelStr = JSON.toJSONString(baseModel);
        //输出
        //设置编码格式
        resp.setCharacterEncoding("utf-8");
        //设置内容
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(baseModelStr);
        //推送出去
        resp.getWriter().flush();
        //关闭
        resp.getWriter().close();
    }
}
